
package panel.viewSpots
{
	import assists.HtmlText;
	import assists.TextFieldUtils;
	import assists.file.FileLoad;
	import assists.log.Log;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MediaEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.media.CameraRoll;
	import flash.media.MediaPromise;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	public class PhotoReadDetail extends Sprite
	{
		private var _desc:TextField;
		private var _backGround:Shape;
		
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _camara:CameraRoll;
		private var _imageLoader:Loader;

		private var _downLoadBtn:ImageResource;
		public function PhotoReadDetail(stage:Stage,loader:ResourceLoader)
		{
			_stage = stage;
			_loader = loader;
			super();
			init();
		}
		
		private function init():void
		{
			_backGround = new Shape();
			this.addChild(_backGround);
			_backGround.graphics.beginFill(0,0.8);
			_backGround.graphics.drawRect(0,0,_stage.stageWidth,150);
			_backGround.graphics.endFill();
			
			_desc = new TextField();
			_desc.htmlText = HtmlText.formatSizeColor("网友上传图片\n美图鉴赏",0xffffff);
			_desc.autoSize = "center";
			_desc.wordWrap = true;
			_desc.multiline = true;
			_desc.width = _stage.stageWidth;
			_desc.mouseEnabled = false;
			this.addChild(_desc);
			
			var heartBtn:ImageResource = new ImageResource(_loader,"assets/viewSpot/photoRead/heart.png");
			heartBtn.x =  _stage.stageWidth / 2 - heartBtn.width * 1.5;
			heartBtn.y = _desc.y + _desc.height;
			this.addChild(heartBtn);
			heartBtn.addEventListener(MouseEvent.CLICK,hearthandler);
			
			_downLoadBtn = new ImageResource(_loader,"assets/viewSpot/photoRead/download.png");
			_downLoadBtn.x =  _stage.stageWidth / 2 + heartBtn.width * 0.5;
			_downLoadBtn.y = _desc.y + _desc.height;
			this.addChild(_downLoadBtn);
			this.addEventListener(MouseEvent.CLICK,downLoadHandler);
			
			_imageLoader = new Loader();
			_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,selectImageSuccess);
			_imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
			this.addChild(_imageLoader);
		}
		
		protected function onLoadError(event:IOErrorEvent):void
		{
			Log.trace("未load取图片成功");
		}
		
		protected function selectImageSuccess(event:Event):void
		{
			var old_width:Number = _imageLoader.width;
			var old_height:Number = _imageLoader.height;
			_imageLoader.width = 360;
			_imageLoader.height = old_width * 360 / old_height;
			_imageLoader.y = _downLoadBtn.y - 360;
			Log.trace("load取图片成功" + _imageLoader.y + "---" + _imageLoader.x);
		}
		
		protected function downLoadHandler(event:MouseEvent):void
		{
//			_downLoadBtn.scaleX = _downLoadBtn.scaleY = 2;
			Log.trace("click download");
			if(CameraRoll.supportsBrowseForImage == true)
			{
				Log.trace("click download1");
			if(_camara == null)
			{
				_camara = new CameraRoll();
				if(CameraRoll.supportsAddBitmapData)
				{
					Log.trace("support add");
				}
				else if(CameraRoll.supportsBrowseForImage)
				{
					Log.trace("browse image");
				}
//				_camara.addEventListener(Event.COMPLETE,saveSuccess);
//				_camara.addEventListener(ErrorEvent.ERROR,saveErrorHandler);
//				var bd:BitmapData = new BitmapData(_stage.stageWidth,_stage.stageHeight);
//				var tt:Bitmap = _loader.getImage("assets/image/test.jpg");
//				bd = tt.bitmapData.clone();
//				_camara.addBitmapData(bd);
				_camara.addEventListener(MediaEvent.SELECT,mediaSelectHandler);
				_camara.addEventListener(Event.CANCEL,medialCancelHandler);
				_camara.addEventListener(ErrorEvent.ERROR,mediaErrorHandler);
		     	_camara.browseForImage();
			}
			}
		}
		
		protected function saveErrorHandler(event:ErrorEvent):void
		{
			Log.trace("-- error");
		}
		
		protected function saveSuccess(event:Event):void
		{
			Log.trace("-- success");
		}
		
		protected function mediaErrorHandler(event:ErrorEvent):void
		{
			Log.trace("调用相册失败");
			
		}
		
		protected function medialCancelHandler(event:Event):void
		{
			Log.trace("cancel");
		}
		
		protected function mediaSelectHandler(event:MediaEvent):void
		{
			var mediaData:MediaPromise = event.data;
			_imageLoader.loadFilePromise(mediaData);
//			var file:FileReference = new FileReference();
//			file.addEventListener(Event.COMPLETE,upCompleteHandler);
//			file.addEventListener(ProgressEvent.PROGRESS,upProgressHandler);
//			file.upload(new URLRequest("http://192.168.0.103"));
			
			var file2:File = mediaData.file;
			file2.addEventListener(Event.COMPLETE,upCompleteHandler);
			file2.addEventListener(ProgressEvent.PROGRESS,upProgressHandler);
			file2.upload(new URLRequest("assets"));
		}
		
		protected function upProgressHandler(event:ProgressEvent):void
		{
			// TODO Auto-generated method stub
			Log.trace("up process" + event.bytesLoaded);
		}
		
		protected function upCompleteHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			Log.trace("up success");
		}
		
		protected function hearthandler(event:MouseEvent):void
		{
			trace("click heart");
			
		}
		
		public function show():void
		{
			TweenLite.to(this,1,{x:0,y:_stage.stageHeight - _desc.height * 2});
		}
		
		public function close():void
		{
			TweenLite.to(this,1,{x:0,y:_stage.stageHeight});
		}
		
		public function changeData(descStr:String):void
		{
			TextFieldUtils.setHtmlText(_desc,HtmlText.formatSizeColor(descStr,0xffffff));
		}
		
	}
}